/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * SearchResourcesView.java
 *
 * Created on 27.05.2013, 19:15:03
 */
package db21;

import db21.entities.Competences;
import db21.entities.EducationalResources;
import db21.entities.Entities;
import db21.entities.Subjects;
import db21.entities.Themes;
import java.util.ArrayList;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JList;

/**
 *
 * @author Mamontov
 */
public class SearchResourcesView extends ApplicationPanel {

    /** Creates new form SearchResourcesView */
    public SearchResourcesView() {
        initComponents();
        JButton btns[] = {btnToCollections,  btnToMain };
        ApplicationMode modes[] = { 
            ApplicationMode.Collections,
            ApplicationMode.MainForm
        };
        for(int i = 0; i < btns.length; i++) {
            this.bindOnClickListenerTogglingToMode(btns[i], modes[i]);
        }
        
         JList lists[] = { 
            lstThemeResources, 
            lstSubjectResource,
            lstCompetenceResources
        };
        for(int i = 0 ; i < lists.length; i++) {
            DefaultListModel m = new DefaultListModel();
            lists[i].setModel(m);
        }
        
    }
    
    @Override
    public void fireModelUpdate() {
        ArrayList<String> subjects = this.m_model.resource(Subjects.class).getList();
        Entities.populateComboBoxWithStrings(cmbSubject, subjects);
        
        ArrayList<String> competences = this.m_model.resource(Competences.class).getList();
        Entities.populateComboBoxWithStrings(cmbСompetence, competences);
        
        ArrayList<String> themes = this.m_model.resource(Themes.class).getList();
        Entities.populateComboBoxWithStrings(cmbTheme, themes);
        
        cmbThemeActionPerformed(null);
        cmbСompetenceActionPerformed(null);
        cmbSubjectActionPerformed(null);       
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        tabtabResources = new javax.swing.JTabbedPane();
        tabByTheme = new javax.swing.JPanel();
        cmbTheme = new javax.swing.JComboBox();
        lstAllResources1 = new javax.swing.JScrollPane();
        lstThemeResources = new javax.swing.JList();
        tabByCompetence = new javax.swing.JPanel();
        cmbСompetence = new javax.swing.JComboBox();
        lstAllResources2 = new javax.swing.JScrollPane();
        lstCompetenceResources = new javax.swing.JList();
        tabBySubjects = new javax.swing.JPanel();
        cmbSubject = new javax.swing.JComboBox();
        lstAllResources3 = new javax.swing.JScrollPane();
        lstSubjectResource = new javax.swing.JList();
        btnToCollections = new javax.swing.JButton();
        btnToMain = new javax.swing.JButton();

        cmbTheme.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmbThemeActionPerformed(evt);
            }
        });

        lstAllResources1.setViewportView(lstThemeResources);

        javax.swing.GroupLayout tabByThemeLayout = new javax.swing.GroupLayout(tabByTheme);
        tabByTheme.setLayout(tabByThemeLayout);
        tabByThemeLayout.setHorizontalGroup(
            tabByThemeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, tabByThemeLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(tabByThemeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lstAllResources1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 359, Short.MAX_VALUE)
                    .addComponent(cmbTheme, javax.swing.GroupLayout.Alignment.LEADING, 0, 359, Short.MAX_VALUE))
                .addContainerGap())
        );
        tabByThemeLayout.setVerticalGroup(
            tabByThemeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(tabByThemeLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(cmbTheme, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lstAllResources1, javax.swing.GroupLayout.DEFAULT_SIZE, 172, Short.MAX_VALUE)
                .addContainerGap())
        );

        tabtabResources.addTab("По темам", tabByTheme);

        cmbСompetence.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmbСompetenceActionPerformed(evt);
            }
        });

        lstAllResources2.setViewportView(lstCompetenceResources);

        javax.swing.GroupLayout tabByCompetenceLayout = new javax.swing.GroupLayout(tabByCompetence);
        tabByCompetence.setLayout(tabByCompetenceLayout);
        tabByCompetenceLayout.setHorizontalGroup(
            tabByCompetenceLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, tabByCompetenceLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(tabByCompetenceLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lstAllResources2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 359, Short.MAX_VALUE)
                    .addComponent(cmbСompetence, javax.swing.GroupLayout.Alignment.LEADING, 0, 359, Short.MAX_VALUE))
                .addContainerGap())
        );
        tabByCompetenceLayout.setVerticalGroup(
            tabByCompetenceLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(tabByCompetenceLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(cmbСompetence, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lstAllResources2, javax.swing.GroupLayout.DEFAULT_SIZE, 172, Short.MAX_VALUE)
                .addContainerGap())
        );

        tabtabResources.addTab("По компетенциям", tabByCompetence);

        cmbSubject.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmbSubjectActionPerformed(evt);
            }
        });

        lstAllResources3.setViewportView(lstSubjectResource);

        javax.swing.GroupLayout tabBySubjectsLayout = new javax.swing.GroupLayout(tabBySubjects);
        tabBySubjects.setLayout(tabBySubjectsLayout);
        tabBySubjectsLayout.setHorizontalGroup(
            tabBySubjectsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, tabBySubjectsLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(tabBySubjectsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lstAllResources3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 359, Short.MAX_VALUE)
                    .addComponent(cmbSubject, javax.swing.GroupLayout.Alignment.LEADING, 0, 359, Short.MAX_VALUE))
                .addContainerGap())
        );
        tabBySubjectsLayout.setVerticalGroup(
            tabBySubjectsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(tabBySubjectsLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(cmbSubject, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lstAllResources3, javax.swing.GroupLayout.DEFAULT_SIZE, 172, Short.MAX_VALUE)
                .addContainerGap())
        );

        tabtabResources.addTab("По предметам", tabBySubjects);

        btnToCollections.setText("К коллекциям");

        btnToMain.setText("К главному окну");
        btnToMain.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnToMainActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addComponent(btnToCollections, javax.swing.GroupLayout.DEFAULT_SIZE, 148, Short.MAX_VALUE)
                .addGap(98, 98, 98)
                .addComponent(btnToMain)
                .addGap(19, 19, 19))
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(8, 8, 8)
                    .addComponent(tabtabResources, javax.swing.GroupLayout.DEFAULT_SIZE, 384, Short.MAX_VALUE)
                    .addGap(8, 8, 8)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(297, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnToMain)
                    .addComponent(btnToCollections))
                .addContainerGap())
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(tabtabResources, javax.swing.GroupLayout.PREFERRED_SIZE, 248, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(72, Short.MAX_VALUE)))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnToMainActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnToMainActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_btnToMainActionPerformed

    private void cmbThemeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmbThemeActionPerformed
        if (cmbTheme.getSelectedIndex() != -1) {
            // lstSubthemesOfTheme
            String subject = cmbTheme.getSelectedItem().toString();
            EducationalResources c = m_model.resource(EducationalResources.class);
            ArrayList<String> s = c.listForTheme(subject);
            Entities.populateListWithStrings(lstThemeResources, s);
        }
    }//GEN-LAST:event_cmbThemeActionPerformed

    private void cmbСompetenceActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmbСompetenceActionPerformed
        if (cmbСompetence.getSelectedIndex() != -1) {
            // lstSubthemesOfTheme
            String subject = cmbСompetence.getSelectedItem().toString();
            EducationalResources c = m_model.resource(EducationalResources.class);
            ArrayList<String> s = c.listForCompetence(subject);
            Entities.populateListWithStrings(lstCompetenceResources, s);
        }
    }//GEN-LAST:event_cmbСompetenceActionPerformed

    private void cmbSubjectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmbSubjectActionPerformed
        if (cmbSubject.getSelectedIndex() != -1) {
            // lstSubthemesOfTheme
            String subject = cmbSubject.getSelectedItem().toString();
            EducationalResources c = m_model.resource(EducationalResources.class);
            ArrayList<String> s = c.listForSubject(subject);
            Entities.populateListWithStrings(lstSubjectResource, s);
        }
    }//GEN-LAST:event_cmbSubjectActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnToCollections;
    private javax.swing.JButton btnToMain;
    private javax.swing.JComboBox cmbSubject;
    private javax.swing.JComboBox cmbTheme;
    private javax.swing.JComboBox cmbСompetence;
    private javax.swing.JScrollPane lstAllResources1;
    private javax.swing.JScrollPane lstAllResources2;
    private javax.swing.JScrollPane lstAllResources3;
    private javax.swing.JList lstCompetenceResources;
    private javax.swing.JList lstSubjectResource;
    private javax.swing.JList lstThemeResources;
    private javax.swing.JPanel tabByCompetence;
    private javax.swing.JPanel tabBySubjects;
    private javax.swing.JPanel tabByTheme;
    private javax.swing.JTabbedPane tabtabResources;
    // End of variables declaration//GEN-END:variables
}
